package com.bs.teachassistant.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bs.teachassistant.R;
import com.bs.teachassistant.adapter.CommDataAdapter;
import com.bs.teachassistant.entity.Note;
import com.bs.teachassistant.utils.GsonUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by limh on 2020/5/3.
 */
@ContentView(R.layout.activity_note_info)
public class NoteInfoActivity extends BaseActivity {
    @ViewInject(R.id.list_note)
    ListView listNote;
    @ViewInject(R.id.txt_info_title)
    TextView txtTitle;
    @ViewInject(R.id.line_null)
    LinearLayout lineNull;

    private CommDataAdapter<Note> adapter;

    private List<Note> currentNotes;
    private String title;

    @Override
    public void initViews() {
        title = getIntent().getStringExtra("classitfy");
        txtTitle.setText(title);
        currentNotes = new ArrayList<>();

    }

    @Override
    public void initDatas() {
        listNote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.setClass(NoteInfoActivity.this, AddRecordActivity.class);
                intent.putExtra("note", currentNotes.get(i));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<Note> allNotes = new ArrayList<>();
        if (!TextUtils.isEmpty(userPreference.getString("notes", ""))) {
            allNotes = GsonUtils.NoteGsonToBean(userPreference.getString("notes", ""));
        }
        currentNotes.clear();
        for (Note item : allNotes) {
            if (title.equals(item.getCalssify())) {
                currentNotes.add(item);
            }
        }

        if (currentNotes.size() > 0) {
            if (null == adapter) {
                adapter = new CommDataAdapter<Note>(currentNotes, R.layout.view_item_note) {
                    @Override
                    public void bindView(ViewHolder holder, Note obj) {
                        holder.setText(R.id.txt_note_title, obj.getTitle());
                        holder.setText(R.id.txt_note_time, obj.getTime());
                        holder.setText(R.id.txt_item_ramark, obj.getRemark());
                        holder.setText(R.id.txt_item_content, obj.getContent());

                        if (TextUtils.isEmpty(obj.getRemark()))
                            holder.setVisibility(R.id.txt_item_ramark, View.GONE);
                        else
                            holder.setVisibility(R.id.txt_item_ramark, View.VISIBLE);
                    }
                };
                listNote.setAdapter(adapter);
            } else {
                adapter.setmData(currentNotes);
            }
        } else {
            lineNull.setVisibility(View.VISIBLE);
        }
    }

    @Event(value = R.id.image_info_close, type = View.OnClickListener.class)
    private void OnClick(View view) {
        switch (view.getId()) {
            case R.id.image_info_close:
                finish();
                overridePendingTransition(R.anim.push_right_in,
                        R.anim.push_right_out);
                break;
        }
    }
}
